//
//  RegionMenuController.m
//  ios-supplier
//
//  Created by ccu on 15/8/26.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "RegionMenuController.h"
#import "JFBRegionDS.h"
#import "JFBStoreInfoDS.h"
#import "JFBAPIClient.h"
@interface RegionMenuController ()

@end

@implementation RegionMenuController


//-(NSMutableArray *)regionArray
//{
//    if (_regionArray == nil) {
//        _regionArray = [NSMutableArray array];
//    }
//    return _regionArray;
//}

-(instancetype)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.regionArray = [@[]mutableCopy];
//   JFBStoreInfoDS *infoDS = [JFBStoreInfoDS loadFromDB];
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    params[@"city_id"] = infoDS.city_id;
//    params[@"shop_id"] = [NSString stringWithFormat:@"%ld",infoDS.ID];
//    params[@"anycode"] = [JFBGlobalSetting getSetting].defaultAnyCode;
//    
//    [[JFBAPIClient sharedClient:false] GET:@"/shop/purchase/address/region.json" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
//        
//        NSArray *regionArray = responseObject[@"data"];
//        
//        for (NSDictionary *regionDict in regionArray) {
//            JFBRegionDS *regionDs = [[JFBRegionDS alloc]initWithAttributes:regionDict];
//            [self.regionArray addObject:regionDs];
//        }
//        
//        NSLog(@"%@",responseObject);
//        
//        [self.tableView reloadData];
//        
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        NSLog(@"%@",error);
//    }];
    
    self.tableView.showsVerticalScrollIndicator = NO;
}

-(void)setRegionArray:(NSMutableArray *)regionArray
{
    if (_regionArray == nil) {
        _regionArray = [@[]mutableCopy];
    }
    _regionArray = regionArray;
    [self.tableView reloadData];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.regionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    JFBRegionDS *region = self.regionArray[indexPath.row];
    cell.textLabel.text = region.name;
    cell.textLabel.textColor = [UIColor colorFromHexValue:0x232323];
    return cell;
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    JFBRegionDS *region = self.regionArray[indexPath.row];
//    
//    NSString * data = self.regionArray[indexPath.row];
//    [self.delegate setleceiveRegion: data];
//    tableView.hidden = YES;
//}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JFBRegionDS *region = self.regionArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(setleceiveRegion:withRegionId:)]) {
        [self.delegate setleceiveRegion: region.name withRegionId: region.ID];
    }
    tableView.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
